package com.bocloud.cmp.driver.providers.ali;

import org.springframework.util.StringUtils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.bocloud.cmp.driver.providers.Driver;
import com.bocloud.cmp.driver.providers.LocationDriver;
import com.bocloud.common.model.BsmResult;

public class AliDriver implements Driver {

	private String accessKey;//
	private String secretKey;//
	private String regionId;//
	private IAcsClient client;
	private IClientProfile profile;

	public String getAccessKey() {
		return accessKey;
	}

	public void setAccessKey(String accessKey) {
		this.accessKey = accessKey;
	}

	public String getSecretKey() {
		return secretKey;
	}

	public void setSecretKey(String secretKey) {
		this.secretKey = secretKey;
	}

	public String getRegionId() {
		return regionId;
	}

	public void setRegionId(String regionId) {
		this.regionId = regionId;
	}

	public IAcsClient getClient() {
		return client;
	}

	public void setClient(IAcsClient client) {
		this.client = client;
	}

	public IClientProfile getProfile() {
		return profile;
	}

	public void setProfile(IClientProfile profile) {
		this.profile = profile;
	}

	public AliDriver() {
		super();
	}

	public AliDriver(String accessKey, String secretKey, String regionId) {
		super();
		this.accessKey = accessKey.trim();
		this.secretKey = secretKey.trim();
		this.regionId = regionId.trim();
		this.profile = DefaultProfile.getProfile(regionId, accessKey, secretKey);
		this.client = new DefaultAcsClient(profile);
	}

	@Override
	public boolean accessable() {
		if (StringUtils.isEmpty(getRegionId())) {
			this.regionId = "cn-hangzhou";
		}
		LocationDriver driver = new AliLocationDriver(accessKey, secretKey, regionId);
		BsmResult result = driver.listRegions();
		return result.isSuccess();
	}
}
